function collectSwitchCase(whilePath,name){
    // 菜老板知识星球获得.
    let ifNodes = [];

    // 遍历WhilePath
    whilePath.traverse({
        "IfStatement"(path)
        {
            //遍历所有的ifStatement;
            let {test,consequent,alternate} = path.node; //获取子节点
            let {left,operator,right} = test; // 必定是BinaryExpression

            if (!types.isIdentifier(left,{name:name}) || operator != '<' || !types.isNumericLiteral(right))
            {//条件过滤
                return;
            }

            let value = right.value;

            //保存整个body，记得生成switchCase节点的时候加上break节点。
            ifNodes[right.value-1] = consequent.body;
            if (!types.isIfStatement(alternate))
            {
                ifNodes[right.value] = alternate.body;  //最后一个else，其实就是上一个else-if 的 test.right的值
            }

        }
    })
    return ifNodes;


}


const if2switchReplace = {
    WhileStatement(path){
        let {test,body} = path.node;
        // `while`循环的参数是`NumericLiteral` ,且内容为`1`.  body中只有2个节点
        if(!types.isNumericLiteral(test,{value:1}) || body.body.length != 2){
            return
        }

        // 判断while循环格式, 条件过滤
        let blockBody = body.body;
        if (!types.isExpressionStatement(blockBody[0]) || !types.isIfStatement(blockBody[1]))
        {
            return;
        }
        // left 左边的节点就是我们需要的变量名
        let {left,right} = blockBody[0].expression; //或者左右节点       aV = cA[wU++];
        let name = left.name;

        // 获取到了变量名称后, 就需要收集使用了aV的case
        let ifNodes = collectSwitchCase(path,name);   //收集case

        //无case，直接返回。
        if (ifNodes.length == 0) return;

        let len = ifNodes.length;
        for (let i=0; i < len; i++)
        {
            //每一个case最后都加break
            ifNodes[i].push(types.BreakStatement());
            ifNodes[i] = types.SwitchCase(test = types.valueToNode(i),consequent = ifNodes[i]);  //生成SwitchCase节点
        }

        //生成SwitchCase节点
        let switchNode = types.SwitchStatement(right,ifNodes);
        path.node.body.body = [switchNode]; //最后的while节点只有一个Switch Node;


    }
}

traverse(ast, if2switchReplace);
